//
//  PlaceDetailViewController.m
//  RoadLine
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 RedFireNoodles. All rights reserved.
//

#import "PlaceDetailViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "PlaceModel.h"
#import "MyButtonView.h"
#import "TextDisplayViewController.h"
#import "PicDisplayViewController.h"
#import "MapViewController.h"
#import "Singleton.h"

// 定义宏
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface PlaceDetailViewController ()
// 详情数据
@property(nonatomic, retain)NSMutableArray *detailsArr;
// 控制页面滚动
@property(nonatomic, retain)UIScrollView *scrollView;
// 大图片
@property(nonatomic, retain)UIImageView *coverImageView;
// 显示景点名字
@property(nonatomic, retain)UILabel *nameLabel;
// 该景点上榜理由
@property(nonatomic, retain)UILabel *reasonLabel;
// 景点描述
@property(nonatomic, retain)MyButtonView *descButton;
// 景点地址
@property(nonatomic, retain)MyButtonView *addressButton;
// 景点联系方法
@property(nonatomic, retain)UILabel *telLabel;
// 路线
@property(nonatomic, retain)MyButtonView *wayButton;
// 开放时间
@property(nonatomic, retain)MyButtonView *openTimeButton;


@end

@implementation PlaceDetailViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.detailsArr = [NSMutableArray array];
    }
    
    return self;
}

// 数据解析
-(void)data{
    NSString *path = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/",self.type,self.placeId];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        PlaceModel *place = [[PlaceModel alloc] init];
        [place setValuesForKeysWithDictionary:dic];
        [self.detailsArr addObject:place];
        // 大图片显示
        for (PlaceModel *place in self.detailsArr) {
            NSMutableArray *arr = place.hottest_places;
            NSString *str = [arr[0] objectForKey:@"photo"];
            NSURL *url = [NSURL URLWithString:str];
            [self.coverImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"BEIJING.png"]];
            self.nameLabel.text = place.name;
            self.reasonLabel.text = place.recommended_reason;
            NSString *text = [NSString stringWithFormat:@"概览:  %@",place.desc];
            self.descButton.label.text = text;
            NSString *address = [NSString stringWithFormat:@"地址:  %@",place.address];
            self.addressButton.label.text = address;
            NSString *way = [NSString stringWithFormat:@"到达方试:  %@",place.arrival_type];
            self.wayButton.label.text = way;
           
            NSString *open_time = [NSString stringWithFormat:@"开放时间:  %@",place.opening_time];
            if (![open_time isEqualToString:@"开放时间:"]) {
                self.openTimeButton.label.text = open_time;
            }else{
            self.openTimeButton.label.text = @"开放时间:  系统提示您这个方式还没有被记录";
            }
            NSString *tel = [NSString stringWithFormat:@"     联系方式:  %@",place.tel];
            if (![tel isEqualToString:@"     联系方式:"]) {
                self.telLabel.text = tel;
            }else{
                self.telLabel.text = @"     联系方式:  系统提示您这个方式还没有被记录.";
            }
        }
      [place release];
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //修改uinavigationBar的字体颜色为白色
    UILabel *titleLabelPrice = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabelPrice.font = [UIFont boldSystemFontOfSize:18];
    titleLabelPrice.textColor = [UIColor whiteColor];
    titleLabelPrice.backgroundColor = [UIColor clearColor];
    titleLabelPrice.text = @"      详情";
    self.navigationItem.titleView = titleLabelPrice;
       
    // 数据解析调用
    [self data];
    
    // 大的scrollView 控制整体滚动
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.scrollView.contentSize = CGSizeMake(0, HEIGHT/3 + 600);
     self.scrollView.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    // 大图片设置
    self.coverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/3)];
    self.coverImageView.backgroundColor = [UIColor colorWithRed:246/255.0 green:85/255.0 blue:91/255.0 alpha:1];
    [self.scrollView addSubview:self.coverImageView];
    self.coverImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.coverImageView.autoresizesSubviews = YES;
    self.coverImageView.clipsToBounds = YES;
    [_coverImageView release];
    self.coverImageView.userInteractionEnabled = YES;
    
    // 添加小图标
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH - 60, HEIGHT/3 - 40, 30, 25)];
    imageView.image = [UIImage imageNamed:@"iconfont-camera"];
    imageView.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:imageView];
    [self.scrollView bringSubviewToFront:imageView];
    [imageView release];
    
    // 设置图片轻拍
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClik:)];
    tap.numberOfTouchesRequired = 1;
    [self.coverImageView addGestureRecognizer:tap];
    [tap release];
    
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT/3, WIDTH, 80)];
    [self.nameLabel setBackgroundColor:[UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1]];
    self.nameLabel.font = [UIFont systemFontOfSize:25];
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    self.nameLabel.layer.cornerRadius = 8;
    [self.scrollView addSubview:self.nameLabel];
    [_nameLabel release];
    
    self.reasonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT/3 + 80, WIDTH, 20)];
    [self.reasonLabel setBackgroundColor:[UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1]];
    self.reasonLabel.font = [UIFont systemFontOfSize:12];
    self.reasonLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:self.reasonLabel];
    [_reasonLabel release];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT/3 + 130, WIDTH, 30)];
    [label setBackgroundColor:[UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1]];
    label.font = [UIFont systemFontOfSize:15];
    label.text = @"基本信息";
    label.textAlignment = NSTextAlignmentCenter;
    label.tag = 10000;
    [self.scrollView addSubview:label];
    [label release];
    
    // 概述
    self.descButton = [[MyButtonView alloc] initWithFrame:CGRectMake(0, 20, WIDTH, 80)];
    self.descButton.label.frame = CGRectMake(20, 0, WIDTH - 40, 80);
    self.descButton.imageView.frame = CGRectMake(WIDTH - 20, 30, 20, 25);
    self.descButton.label.numberOfLines = 4;
    self.descButton.label.font = [UIFont systemFontOfSize:12];
    [self.descButton setBackgroundColor:[UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1]];
    [self.scrollView addSubview:self.descButton];
    [self.descButton addNewTarget:self action:@selector(ButtonClik:)];
    [_descButton release];
    
    // 地址
    self.addressButton = [[MyButtonView alloc] initWithFrame:CGRectMake(0, 110, WIDTH, 40)];
    [self.addressButton setBackgroundColor:[UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1]];
    self.addressButton.label.frame = CGRectMake(20, 0, WIDTH - 40, 40);
    self.addressButton.imageView.frame = CGRectMake(WIDTH - 20, 10, 20, 25);
    self.addressButton.label.font = [UIFont systemFontOfSize:12];
    [_addressButton release];
    [self.addressButton addNewTarget:self action:@selector(ButtonClik:)];
    
    // 交通方式
    self.wayButton = [[MyButtonView alloc] initWithFrame:CGRectMake(0, 160, WIDTH, 90)];
    self.wayButton.label.frame = CGRectMake(20, 0, WIDTH - 40, 90);
    [self.wayButton setBackgroundColor:[UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1]];
    self.wayButton.imageView.frame = CGRectMake(WIDTH - 20, 35, 20, 25);
    self.wayButton.label.font = [UIFont systemFontOfSize:12];
    self.wayButton.label.numberOfLines = 4;
    [_wayButton release];
    [self.wayButton addNewTarget:self action:@selector(ButtonClik:)];

    // 开放时间
    self.openTimeButton = [[MyButtonView alloc] initWithFrame:CGRectMake(0, 260, WIDTH, 25)];
    self.openTimeButton.label.frame = CGRectMake(20, 0, WIDTH - 40, 25);
    self.openTimeButton.imageView.frame = CGRectMake(WIDTH - 20, 0, 20, 25);
    self.openTimeButton.label.font = [UIFont systemFontOfSize:12];
    [_openTimeButton release];
    [self.openTimeButton setBackgroundColor:[UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1]];
    [self.openTimeButton addNewTarget:self action:@selector(ButtonClik:)];
    
    // 电话号码
    self.telLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 290, WIDTH, 20)];
    self.telLabel.font = [UIFont systemFontOfSize:12];
    [_telLabel release];
    [self.telLabel setBackgroundColor:[UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1]];
   
    
    // 辅助视图 概述、地址、交通方式等都铺在这上面
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT/3 + 170, WIDTH, 310)];
    view.tag = 10001;
    view.backgroundColor = [UIColor clearColor];
    [view addSubview:self.descButton];
    [view addSubview:self.openTimeButton];
    [view addSubview:self.telLabel];
    [view addSubview:self.wayButton];
    [view addSubview:self.addressButton];
    [self.scrollView addSubview:view];
    [view release];
    
    
    if ([Singleton shareSingleton].isNight) {
        [[Singleton shareSingleton] loadNightModeInfoFormDisk];
    }
}



// 各自定义Button的方法
-(void)ButtonClik:(MyButtonView *)button{
    // 跳转到文本显示页面显示文本
    if (button == self.descButton) {
        TextDisplayViewController *text = [[TextDisplayViewController alloc] init];
        [self.navigationController pushViewController:text animated:YES];
        text.str = self.descButton.label.text;
        text.placeName = self.nameLabel.text;
        [text release];
    }
    // 跳转到地图页面
    else if (button == self.addressButton){
        MapViewController *map = [[MapViewController alloc] init];
        PlaceModel *place = [self.detailsArr lastObject];
        map.location = place.location;
        [self.navigationController pushViewController:map animated:YES];
        [map release];
    }
    // 跳转到文本显示页面显示文本
    else if (button == self.wayButton){
        TextDisplayViewController *text = [[TextDisplayViewController alloc] init];
        [self.navigationController pushViewController:text animated:YES];
        text.str = self.wayButton.label.text;
        text.placeName = self.nameLabel.text;
        [text release];
    }
    // 跳转到文本显示页面显示文本
    else{
        TextDisplayViewController *text = [[TextDisplayViewController alloc] init];
        [self.navigationController pushViewController:text animated:YES];
        text.str = self.openTimeButton.label.text;
        text.placeName = self.nameLabel.text;
        [text release];
    }
}

// 设置白天模式
- (void)setLightDayModel {
    
    self.view.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    self.scrollView.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    self.nameLabel.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    self.nameLabel.textColor = [UIColor blackColor];
    self.reasonLabel.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    self.reasonLabel.textColor = [UIColor blackColor];
    self.descButton.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    self.openTimeButton.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    self.wayButton.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    self.telLabel.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    self.telLabel.textColor = [UIColor blackColor];
    self.addressButton.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    UILabel *label = (UILabel *)[self.scrollView viewWithTag:10000];
    label.backgroundColor = [UIColor colorWithRed:250/255.0 green:245/255.0 blue:232/255.0 alpha:1];
    label.textColor = [UIColor blackColor];
    UIView *view = [self.scrollView viewWithTag:10001];
    view.backgroundColor = [UIColor clearColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:246/255.0 green:85/255.0 blue:91/255.0 alpha:1];
}

// 设置夜间模式
- (void)setNightDayModel {
    
    self.view.backgroundColor = [UIColor blackColor];
    self.scrollView.backgroundColor = [UIColor blackColor];
    self.nameLabel.backgroundColor = [UIColor blackColor];
    self.nameLabel.textColor = [UIColor whiteColor];
    self.reasonLabel.backgroundColor = [UIColor blackColor];
    self.reasonLabel.textColor = [UIColor whiteColor];
    self.descButton.backgroundColor = [UIColor blackColor];
    self.openTimeButton.backgroundColor = [UIColor blackColor];
    self.wayButton.backgroundColor = [UIColor blackColor];
    self.telLabel.backgroundColor = [UIColor blackColor];
    self.telLabel.textColor = [UIColor whiteColor];
    self.addressButton.backgroundColor = [UIColor blackColor];
    UILabel *label = (UILabel *)[self.scrollView viewWithTag:10000];
    label.backgroundColor = [UIColor blackColor];
    label.textColor = [UIColor whiteColor];
    UIView *view = [self.scrollView viewWithTag:10001];
    view.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:4/255.0 green:165/255.0 blue:171/255.0 alpha:1];
}

// 点击图片实现跳转（详情）
-(void)tapClik:(UITapGestureRecognizer *)tap{
    PicDisplayViewController *pic = [[PicDisplayViewController alloc] init];
    [self.navigationController pushViewController:pic animated:YES];
    pic.type = self.type;
    pic.pic_id = self.placeId;
    pic.name = self.nameLabel.text;
    [pic release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    // 详情数据
    [_detailsArr release];
    // 控制页面滚动
    [_scrollView release];
    // 大图片
    [_coverImageView release];
    // 显示景点名字
    [_nameLabel release];
    // 该景点上榜理由
    [_reasonLabel release];
    // 景点描述
    [_descButton release];
    // 景点地址
    [_addressButton release];
    // 景点联系方法
    [_telLabel release];
    // 路线
    [_wayButton release];
    // 开放时间
    [_openTimeButton release];
    
    [super dealloc];
}

@end
